package com.qk.management.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.code.ParamEnum;
import com.qk.dto.customer.CustomerDTO;
import com.qk.entity.Customer;
import com.qk.exception.BusinessException;
import com.qk.management.annotation.LogAnno;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import com.qk.result.PageResult;
import com.qk.vo.customer.CustomerVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author:zhengbowen
 * @Date: 2025/10/14 16:49
 * @Description:
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Override
    public PageResult<CustomerVO> selectCustomersByPage(CustomerDTO customerDTO) {
        PageHelper.startPage(customerDTO.getPage(), customerDTO.getPageSize());
        List<CustomerVO> customerVOS = baseMapper.selectCustomersByPage(customerDTO);
        PageInfo<CustomerVO> pageInfo = new PageInfo<>(customerVOS);
        return PageResult.<CustomerVO>builder()
                .total(pageInfo.getTotal())
                .rows(pageInfo.getList())
                .build();
    }

    @Override
    @LogAnno
    public void insertCustomer(Customer customer) {
        if(!(ObjectUtil.isNotNull(customer)&&ObjectUtil.isNotNull(customer.getPhone())))
        {
            throw new BusinessException(ParamEnum.PARAM_ERROR);
        }
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        baseMapper.insert(customer);
    }

    @Override
    public void updateDataById(Customer customer) {
        customer.setUpdateTime(LocalDateTime.now());
        baseMapper.updateById(customer);
    }
}
